package com.pangyangyang.elasticsearch.plugin.jdbc.handler;

import com.pangyangyang.elasticsearch.plugin.event.DataModifyEvent;
import com.pangyangyang.elasticsearch.plugin.handler.AbstractEventHandler;
import com.pangyangyang.elasticsearch.plugin.jdbc.constant.JDBCHandlerConstants;
import com.pangyangyang.elasticsearch.plugin.jdbc.handler.action.JDBCQueryHandlerAction;
import com.pangyangyang.elasticsearch.plugin.jdbc.query.QueryDataEntity;
import com.pangyangyang.elasticsearch.plugin.river.MessageRiverSettings;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.logging.ESLogger;

/**
 * Created by yangwang on 2015/1/22.
 */
public class JDBCQueryHandler extends AbstractEventHandler {

    private JDBCQueryHandlerAction insertAction, updateAction, deleteAction;

    public JDBCQueryHandler(Client client, MessageRiverSettings settings) {
        super(client, settings);
        if (settings.getObject(JDBCHandlerConstants.SETTING_KEY_EVENT_INSERT) != null){
            insertAction = new JDBCQueryHandlerAction(JDBCHandlerConstants.SETTING_KEY_EVENT_INSERT, settings, this);
        }
        if (settings.getObject(JDBCHandlerConstants.SETTING_KEY_EVENT_UPDATE) != null){
            updateAction = new JDBCQueryHandlerAction(JDBCHandlerConstants.SETTING_KEY_EVENT_UPDATE, settings, this);
        }
        if (settings.getObject(JDBCHandlerConstants.SETTING_KEY_EVENT_DELETE) != null){
            deleteAction = new JDBCQueryHandlerAction(JDBCHandlerConstants.SETTING_KEY_EVENT_DELETE, settings, this);
        }
        logger.debug("initialized jdbc handler,  actions: {} {} {}.", insertAction, updateAction, deleteAction);
    }

    /*
     * 注册jdbc connection
      * 初始化Executor
     */
    @Override
    protected void doStart() {
        if (insertAction != null)
            insertAction.start();
        if (deleteAction != null)
            deleteAction.start();
        if (updateAction != null)
            updateAction.start();
    }

    @Override
    public void handle(DataModifyEvent event) {

        switch (event.getType()){
            case INSERT:
                executeAction(insertAction, event);
                break;
            case UPDATE:
                executeAction(updateAction, event);
                break;
            case DELETE:
                executeAction(deleteAction, event);
                break;
        }
    }

    @Override
    public void stop() {
        if (insertAction != null)
            insertAction.stop();
        if (deleteAction != null)
            deleteAction.stop();
        if (updateAction != null)
            updateAction.stop();
    }

    @Override
    public boolean indexDocument(QueryDataEntity entity) {
        return super.indexDocument(entity);
    }

    @Override
    public boolean indexDocument(String index, String type, QueryDataEntity entity) {
        return super.indexDocument(index, type, entity);
    }

    @Override
    public boolean deleteDocument(String id) {
        return super.deleteDocument(id);
    }

    @Override
    public boolean deleteDocument(String index, String type, String id) {
        return super.deleteDocument(index, type, id);
    }

    private boolean executeAction(JDBCQueryHandlerAction action, DataModifyEvent event){
        if (action != null)
            return action.execute(event);

        return false;
    }
}
